package com.misboot.document.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.misboot.document.client.SystemClient;
import com.misboot.document.model.SysUser;
import com.misboot.document.model.UserEntity;
import com.misboot.document.service.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author zm
 * 用户相关回调接口
 */
@Controller
@RequestMapping("v1/3rd/user")
public class UserCallBackController extends DocumentBaseController {

    @Autowired
    private SystemClient systemClient;

    static Logger logger = LoggerFactory.getLogger(UserCallBackController.class);

    /**
     * 获取用户信息
     */
    @ResponseBody
    @PostMapping("info")
    public Object userInfo(
            @RequestBody JSONObject reqObj
    ) {
        logger.info("获取用户信息param:{}", JSON.toJSON(reqObj));
        try {
            Map<String, Object> map = getUserInfo(reqObj);
            HashMap var3 = new HashMap();
            var3.put("statusCode", 200);
            var3.put("title", "操作提示");
            var3.put("message", "获取成功");
            var3.put("map", map);
            return var3;
        } catch (Exception e) {
            return failure("获取用户信息异常");
        }
    }

    public Map<String, Object> getUserInfo(JSONObject reqObj) {
        List<String> ids = null;

        if (reqObj != null) {
            if (reqObj.containsKey("ids")) {
                String str = reqObj.getString("ids").replace("[", "").replace("]", "");
                ids = Arrays.asList(str);
            }
        }

        Map<String, Object> map = new HashMap<>();
        List<UserEntity> users = new ArrayList<>();
        if (ids != null && !ids.isEmpty()) {
            UserEntity user = null;
            for (String id : ids) {
                Object object = systemClient.getUserByUserNameId("ewsd0001");
                String str = JSON.toJSONString(object);
                SysUser sysUser = com.alibaba.fastjson.JSONObject.parseObject(str, SysUser.class);
                user = new UserEntity();
                user.setId(sysUser.getUserNameId());
                user.setName(sysUser.getUserName());
                user.setAvatar_url(sysUser.getUserHeadPortrait());
                if (user != null) {
                    users.add(user);
                }
            }
        }
        map.put("users", users);

        return map;
    }

}
